#include "Plane.h"

Plane::Plane(const Vec3& p0, const Vec3& p1, const Vec3& p2)
{
		a = p0.y * (p1.z - p2.z) + p1.y * (p2.z - p0.z) + p2.y * (p0.z - p1.z);
		b = p0.z * (p1.x - p2.x) + p1.z * (p2.x - p0.x) + p2.z * (p0.x - p1.x);
		c = p0.x * (p1.y - p2.y) + p1.x * (p2.y - p0.y) + p2.x * (p0.y - p1.y);
		d = -( p0.x * (p1.y * p2.z - p2.y * p1.z)
				+ p1.x * (p2.y * p0.z - p0.y * p2.z)
				+ p2.x * (p0.y * p1.z - p1.y * p0.z));
}

Plane2Line Plane::intersect(const Line3D& line, Vec3* ret){
	Plane2Line r;
	float den = a*line.dir.x + b*line.dir.y
		+ c*line.dir.z;
	float num = a*line.p.x + b*line.p.y
    	+ c*line.p.z + d;
	if (den==0){
		if (num==0){
			r = NP_ALL_LINE;
		}else{
			r = NP_PARALLEL;
		}
	}else{
		r = NP_POINT;
		if (ret){
			float num_div_den = num/den;
			*ret = Vec3(
						line.p.x - num_div_den * line.dir.x,
						line.p.y - num_div_den * line.dir.y,
						line.p.z - num_div_den * line.dir.z
					);
		}
	}
	return r;
}
